def prim(N, grid):
    isintree = [False] * (N+1)
    minDist = [10001] * (N+1)
    
    for i in range(1, N):
        
        minvalue = min([minDist[x] for x in range(N+1) if not isintree[x] ])
        cur = 0
        for j in range(1, N+1):
            if not isintree[j] and minDist[j] == minvalue:
                isintree[j] = True
                cur = j
                break
        
        for j in range(1, N+1):
            if not isintree[j] and grid[cur][j]<minDist[j]:
                minDist[j] = grid[cur][j]
        
    return sum(minDist[2:])


def main():
    N, edge = map(int, input().split())
    
    grid = [[10001]*(N+1) for _ in range(N+1)]
    for i in range(edge):
        s, t, value = map(int, input().split())
        grid[s][t] = value
        grid[t][s] = value
    
    mindist = prim(N, grid)
    print(mindist)
    
main()